package com.thoughtworks.collection;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.*;

public class Add {
    public int getSumOfEvens(int leftBorder, int rightBorder) {
//        int sum = 0;
//        for (int i = leftBorder; i < rightBorder;i++){
//            if (i % 2 == 0){
//                sum += i;
//            }
//        }
//        return sum;
        int a = 0;
        int b = 0;
        int c = 0;
        if (leftBorder > rightBorder){
            a = leftBorder;
            b = rightBorder;
        }
        else {
            a = rightBorder;
            b = leftBorder;
        }
        for (int i = b;i <= a;i++){
            if (i % 2 ==0){
                c = i + c;
            }
        }
        return c;
    }

    public int getSumOfOdds(int leftBorder, int rightBorder) {
//        int sum = 0;
//        for (int i = leftBorder; i < rightBorder;i++){
//            sum += i % 2 == 1 ? i : 0;
//        }
//        return sum;
        int a = 0;
        int b = 0;
        int c = 0;
        if (leftBorder > rightBorder){
            a = leftBorder;
            b = rightBorder;
        }
        else {
            a = rightBorder;
            b = leftBorder;
        }
        for (int i = b;i <= a;i++){
            if (i % 2 ==1){
                c = i + c;
            }
        }
        return c;
    }


    public int getSumTripleAndAddTwo(List<Integer> arrayList) {
        int sum =0;
        for (Integer integer : arrayList){
            sum += integer * 3 +2;
        }
        return sum;
    }

    public List<Integer> getTripleOfOddAndAddTwo(List<Integer> arrayList) {
       List list = new ArrayList();
       for (Integer i : arrayList){
           if (i % 2 == 1){
               i= i * 3 + 2;
           }
           list.add(i);
       }
       return list;
    }

    public int getSumOfProcessedOdds(List<Integer> arrayList) {
       int sum = 0;
        for (Integer i : arrayList){
            if (i % 2 ==1){
                sum = (i * 3 + 5) + sum;
            }
        }
        return sum;
//        Integer[] integer = (Integer[]) arrayList.toArray();
   }

    public double getMedianOfEven(List<Integer> arrayList) {
        List<Integer> list = new ArrayList<>();
        double sum = 0;
        int a = 1;
        for (Integer i : arrayList) {
            if (i % 2 == 0) {
                list.add(i);
            }
        }
        if (list.size() % 2 == 0) {
            sum = (list.get(list.size() / 2 - 1) + list.get(list.size() / 2)) / 2;
        } else {
            sum = list.get(list.size() / 2 + 1);
        }

        return sum;
    }

    public double getAverageOfEven(List<Integer> arrayList) {
        int sum = 0;
        double a = 0;
        for (Integer i : arrayList) {
            sum = i + sum;
        }
        a = sum / arrayList.size();
        return a;
    }



    public boolean isIncludedInEvenIndex(List<Integer> arrayList, Integer specialElment) {
        List<Integer> list = new ArrayList<>();
        for ( Integer i:arrayList) {
            if (i%2==0){
                list.add(i);
            }
        }
        boolean a= false;
        for (Integer b:list) {
            if(b==specialElment){
                a= true;
            }
        }
        return a;
    }

    public List<Integer> getUnrepeatedFromEvenIndex(List<Integer> arrayList) {
        List<Integer> list = new ArrayList<>();
        for ( Integer i:arrayList) {
            if (i%2==0){
                list.add(i);
            }
        }
        Set<Integer> middleHashSet = new HashSet<Integer>(list);

        List<Integer> afterHashSetList = new ArrayList<Integer>(middleHashSet);
        return  afterHashSetList;
    }


    public List<Integer> sortByEvenAndOdd(List<Integer> arrayList) {
        List<Integer> odd = new ArrayList<Integer>();
        List<Integer> even = new ArrayList<Integer>();

        int temp = 0;
        for (int j=0; j<arrayList.size(); j++) {
            temp= arrayList.get(j);
            if(temp%2==0) {
                even.add(temp);
            }else {
                odd.add(temp);
            }
            Collections.sort(even);
            Collections.sort(odd, Collections.reverseOrder());

            int m = 0;
            for(int i=0; i < even.size(); i++){
                arrayList.set(m, even.get(i));
                m++;
            }
            for(int jj = 0;jj < odd.size();jj++){
                arrayList.set(m, odd.get(jj));
                m++;
            }
        }
        return arrayList;
    }


    public List<Integer> getProcessedList(List<Integer> arrayList) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < arrayList.size() -1;i++){
            int a = (arrayList.get(i) + arrayList.get(i + 1)) * 3;
            list.add(a);
        }
        return list;
    }
}
